package com.googlecode.grs.mechanism;

import com.googlecode.grs.core.controller.EventController;
import com.googlecode.grs.event.RangeFinderEvent;
import com.googlecode.grs.event.RangeFinderListener;

/**
 * A sonarArray reads raw sensor data, filters it.
 * 
 * @author ajc
 * 
 */
public class SonarArray extends EventController implements RangeFinderListener {

	@Override
	public void startListening() {

	}

	@Override
	public void stopListening() {

	}

	@Override
	public void halt() {

	}

	@Override
	public void pause() {

	}

	@Override
	public void unPause() {

	}

	/**
	 * Smooth the data using a simple boxcar or triangle filter.
	 * 
	 * @param i
	 *            the index of the data sample to compute
	 * @param raw
	 *            the array containing the data values
	 * @return the smoothed value for point at index
	 */
	private double smooth(int i, double[] raw) {
		System.out.println(raw.length);
		// if (indexMax <= 0) return 0;
		int i0 = i - 5;
		int i1 = i + 5;
		int wt = 1;
		double r = 0.0;
		for (int j = i0; j <= i1; j++) {
			// r += wt * raw[j];
			r += wt * raw[getJIndex(j, raw)];

			// if < 0, 0. if greater than max index, max index -1.
			// r += wt * raw[(j <= 0) ? 0 : (j >= indexMax) ? (indexMax - 1) :
			// j];
			// if (!boxcar) {
			// if (j < i) {
			// wt += 2;
			// } else {
			// wt -= 2;
			// }
			// }z
		}
		return r / (10 + 1);
	}

	private int getJIndex(int j, double[] raw) {
		if (j >= raw.length) {
			return raw.length - 1;
		}
		if (j < 0) {
			return 0;
		}
		return j;

	}

	@Override
	public void distanceChanged(RangeFinderEvent e) {
		// TODO Auto-generated method stub

	}

}
